<?php

namespace app\api\controller;

use app\admin\model\AskForLeave;
use app\admin\model\ClassAddress;
use app\admin\model\ClockIn;
use app\admin\model\Club;
use app\admin\model\ClubDtl;
use app\admin\model\Notice;
use app\admin\model\TeacherClass;
use app\admin\model\TeacherClassDtl;
use app\admin\model\TeacherCourseDisplay;
use app\admin\model\TeacherDetail;
use app\common\controller\Api;

/**
 * 消息接口
 */
class NoticeApi extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    //消息列表
    public function notice_list(){
        $params = $this->request->param();
        if(!isset($params['page'])||!$params['page']){
            $params['page'] = 1;
        }
        if(!isset($params['page_size'])||!$params['page_size']){
            $params['page_size'] = 10;
        }

        $user = $this->auth->getUser();
        if(!isset($user['id'])||!$user['id']){
            $this->error('当前登录用户不存在');
        }

        $query = Notice::where(['get_user_id'=>$user['id']])->order('is_read','asc')->order('id','desc')->paginate($params['page_size']);

        $list = $query->getCollection();
        $arr = [];
        foreach ($list as $k=>$v){
            $arr[$k]['id'] = $v['id'];
            $arr[$k]['teacher_class_id'] = $v['teacher_class_id'];
            $arr[$k]['teacher_class_dtl_id'] = $v['teacher_class_dtl_id'];
            $arr[$k]['title'] = $v['title'];
            $arr[$k]['content'] = $v['content'];
            $arr[$k]['need_confirm'] = $v['need_confirm'];
            if($v['teacher_class_id']){
                $arr[$k]['jump'] = $v['jump'];
            }else{
                $arr[$k]['jump'] = '';
            }
            $arr[$k]['addtime'] = $v['addtime'];
            $arr[$k]['confirm_status'] = $v['confirm_status'];
            $arr[$k]['vacation_id'] = $v['sf_id'];
            if($v['need_confirm'] == 1){
                if($v['jump'] == '课程详情'&&$v['confirm_status'] != 1){
                    $arr[$k]['need_confirm'] = 2;
                }
            }
            if($v['jump'] == '课程详情请假'){
                $ask = AskForLeave::where(['id'=>$v['sf_id']])->find();
                if(isset($ask['status'])&&$ask['status'] == 1){
                    $arr[$k]['need_confirm'] = 1;
                }else{
                    $arr[$k]['need_confirm'] = 2;
                }
            }
            if($v['jump'] == '课程详情补课'&&$v['confirm_status'] != 1){
                $arr[$k]['need_confirm'] = 2;
            }
            $arr[$k]['clock_in_img'] = '';
            $arr[$k]['clock_in_remark'] = '';
            $arr_ci_m = [];
            $arr[$k]['clock_in'] = $arr_ci_m;
            if($v['jump'] == '课程详情'||$v['jump'] == '课程详情补课'){
                $sql = "(title like '%当日课程确认' or title like '%当日补课确认') and identity_type = '校方' and teacher_class_id = {$v['teacher_class_id']}";
                $n_m = Notice::where($sql)->order('id','asc')->select();
                $id_sort = 1;
                foreach ($n_m as $kk=>$vv){
                    if($v['id'] == $vv['id']){
                        $dateStr = substr($vv['addtime'], 0, 10);
//                        $ci_m = ClockIn::where(['class_id'=>$v['teacher_class_id'], 'date' => $dateStr])->page($id_sort,1)->find();
//                        $arr[$k]['clock_in_img'] = isset($ci_m['img']) ? $ci_m['img'] : '';
//                        $arr[$k]['clock_in_remark'] = isset($ci_m['remark']) ? $ci_m['remark'] : '';
                        $ci_m = ClockIn::where(['class_id'=>$v['teacher_class_id'], 'date' => $dateStr])->order('id','asc')->select();

                        foreach ($ci_m as $kkk=>$vvv){
                            if($vvv['is_sk'] == 1){
                                $arr_ci_m[$kkk]['type'] = '上课打卡';
                            }else{
                                $arr_ci_m[$kkk]['type'] = '下课打卡';
                            }
                            $arr_ci_m[$kkk]['clock_in_img'] = isset($vvv['img']) ? $vvv['img'] : '';
                            $arr_ci_m[$kkk]['clock_in_remark'] = isset($vvv['remark']) ? $vvv['remark'] : '';
                            $arr_ci_m[$kkk]['clock_in_addtime'] = isset($vvv['addtime']) ? $vvv['addtime'] : '';
                        }
                        $arr[$k]['clock_in'] = $arr_ci_m;
                    }else{
                        $id_sort++;
                    }
                }
            }
            $arr[$k]['club_log'] = '';
            if($v['teacher_class_id']){
                $tc = TeacherClass::where(['id'=>$v['teacher_class_id']])->find();
                if(isset($tc['teacher_detail_id'])&&$tc['teacher_detail_id']){
                    $td = TeacherDetail::where(['id'=>$tc['teacher_detail_id']])->find();
                    if(isset($td['club_two_id'])&&$td['club_two_id']){
                        $cd = ClubDtl::where(['id'=>$td['club_two_id']])->find();
                        if(isset($cd['img_url'])&&$cd['img_url']){
                            $arr[$k]['club_log'] = $cd['img_url'];
                        }
                    }
                }
            }
            $arr[$k]['is_read'] = $v['is_read'];
        }
        $count = $query->total();
        $data['list'] = $arr;
        $data['count'] = $count;
        $this->success('课程列表',$data);
    }

    //获取消息提醒
    public function get_notice_tip(){
        $user = $this->auth->getUser();
        if(!isset($user['id'])||!$user['id']){
            $this->error('当前登录用户不存在');
        }
        $count = Notice::where(['get_user_id'=>$user['id'],'is_read'=>1])->count();
        $this->success('未读消息数',$count);
    }

    //消息确认
    public function confirm_notice(){
        $params = $this->request->param();
        if(!isset($params['id'])||!$params['id']){
            $this->error('消息id必传');
        }
        if(!isset($params['teacher_class_dtl_id'])||!$params['teacher_class_dtl_id']){
            $this->error('安排的课程id必传');
        }
        if(!isset($params['confirm_status'])||!$params['confirm_status']){
            $this->error('确认状态必传');
        }
        $find = Notice::where(['id'=>$params['id']])->find();
        $msg = '';
        Notice::where(['id'=>$params['id']])->update(['is_read'=>2]);
        switch ($params['confirm_status']){
            case 2:
                //确认
                $msg = '已确认';
                Notice::where(['id'=>$params['id']])->update(['confirm_status'=>2]);
                $send_date = date('Y-m-d',strtotime($find['addtime']));
                $date = date('Y-m-d');
                $week = date('N');
                if($send_date!=$date&&$week==1){
                    TeacherClassDtl::jd($params['teacher_class_dtl_id'],$find['teacher_class_id'],$find['class_num'],1);
                }else{
                    TeacherClassDtl::jd($params['teacher_class_dtl_id'],$find['teacher_class_id'],$find['class_num']);
                }
                break;
            case 3:
                //驳回
                $msg = '已驳回';
                Notice::where(['id'=>$params['id']])->update(['confirm_status'=>3]);
                $class = TeacherClass::where(['id'=>$find['teacher_class_id']])->find();
                //给教师发被驳回
                $send_add_notice = [];
                $send_add_notice['title'] = '课程确认消息';//标题
                $send_add_notice['content'] = "您的课程未按计划完成";//内容
                $send_add_notice['need_confirm'] = 2;//是否需要确认 1是 2否
                $send_add_notice['jump'] = '课程详情';//跳转地址
                $send_add_notice['teacher_class_id'] = $find['teacher_class_id'];//课程id
                $send_add_notice['teacher_class_dtl_id'] = 0;//课程安排id
                $send_add_notice['send_user_id'] = $class['user_id'];//发送人id
                $send_add_notice['get_user_id'] = $class['teacher_id'];//接收人id
                $send_add_notice['identity_type'] = '教师';//接收人身份
                $send_add_notice['addtime'] = date('Y-m-d H:i:s');//添加时间
                $send_add_notice['is_read'] = 1;//是否已读 1未读 2已读
                Notice::insertGetId($send_add_notice);
                // 发送个推消息
                Notice::sendGetuiPush($send_add_notice);
                break;
            default:
                $this->error('确认状态参数异常');
                break;
        }
        $this->success('消息确认',$msg);
    }

    //补课消息确认
    public function bukeconfirm__notice(){
        $params = $this->request->param();
        if(!isset($params['id'])||!$params['id']){
            $this->error('消息id必传');
        }
        if(!isset($params['confirm_status'])||!$params['confirm_status']){
            $this->error('确认状态必传');
        }
        $find = Notice::where(['id'=>$params['id']])->find();
        if($find['jump']!='课程详情补课'){
            $this->error('非补课不可请求');
        }
        $msg = '';
        Notice::where(['id'=>$params['id']])->update(['is_read'=>2]);
        switch ($params['confirm_status']){
            case 2:
                //确认
                $msg = '已确认';
                Notice::where(['id'=>$params['id']])->update(['confirm_status'=>2]);
                TeacherClassDtl::jd(0,$find['teacher_class_id'],$find['class_num'],1);
                if($find['jump'] == '课程详情补课'){
                    if($find['sf_id']){
                        AskForLeave::where(['id'=>$find['sf_id']])->update(['is_wc'=>1]);
                    }
                }
                break;
            case 3:
                //驳回
                $msg = '已驳回';
                Notice::where(['id'=>$params['id']])->update(['confirm_status'=>3]);
                $class = TeacherClass::where(['id'=>$find['teacher_class_id']])->find();
                //给教师发被驳回
                $send_add_notice = [];
                $send_add_notice['title'] = '补课课程确认消息';//标题
                $send_add_notice['content'] = "您的补课课程未按计划完成";//内容
                $send_add_notice['need_confirm'] = 2;//是否需要确认 1是 2否
                $send_add_notice['jump'] = '课程详情';//跳转地址
                $send_add_notice['teacher_class_id'] = $find['teacher_class_id'];//课程id
                $send_add_notice['teacher_class_dtl_id'] = 0;//课程安排id
                $send_add_notice['send_user_id'] = $class['user_id'];//发送人id
                $send_add_notice['get_user_id'] = $class['teacher_id'];//接收人id
                $send_add_notice['identity_type'] = '教师';//接收人身份
                $send_add_notice['addtime'] = date('Y-m-d H:i:s');//添加时间
                $send_add_notice['is_read'] = 1;//是否已读 1未读 2已读
                Notice::insertGetId($send_add_notice);
                // 发送个推消息
                Notice::sendGetuiPush($send_add_notice);
                break;
            default:
                $this->error('确认状态参数异常');
                break;
        }
        $this->success('消息确认',$msg);
    }
    public function get_week_day($time){
        $week_array = array('天','一','二','三','四','五','六');
        $week = date('w',strtotime($time));
        return "周".$week_array[$week];
    }
    //审核请假
    public function audit_vacation(){
        $params = $this->request->param();
        if(!isset($params['id'])||!$params['id']){
            $this->error('消息id必传');
        }
        if(!isset($params['vacation_id'])||!$params['vacation_id']){
            $this->error('请假id必传');
        }
        if(!isset($params['vacation_status'])||!$params['vacation_status']){
            $this->error('确认状态必传');
        }
        $find = Notice::where(['id'=>$params['id']])->find();
        $tc = TeacherClass::where(['id'=>$find['teacher_class_id']])->find();

        $msg = '';
        Notice::where(['id'=>$params['id']])->update(['is_read'=>2]);
        switch ($params['vacation_status']){
            case 2:
                if($tc['status'] != 1){
                    $this->error('课程已完成或已中止不可请假');
                }
                //确认
                $msg = '已通过';
                AskForLeave::where(['id'=>$params['vacation_id']])->update(['status'=>2]);
                $ask = AskForLeave::where(['id'=>$params['vacation_id']])->find();
                $tcd = TeacherClassDtl::where(['teacher_class_id'=>$find['teacher_class_id']])->select();
                foreach ($tcd as $k=>$v){
                    $str = $v['week_date'];
                    $subStr = "周";
                    if (strstr($str, $subStr)) {
                        $start_time = $this->get_week_day($ask['start_time']);
                        if($str == $start_time){
                            if($v['status'] == 2){
                                $this->error('课程已完成，该请假已过期');
                            }
                            //相同则改为已请假
                            TeacherClassDtl::where(['id'=>$v['id']])->update(['status'=>3]);
                            AskForLeave::where(['id'=>$params['vacation_id']])->update(['bk_time'=>$v['time_start'],'bk_class_num'=>$v['class_num']]);
                        }
                    } else {
                        if($v['week_date'] == $ask['start_time']){
                            if($v['status'] == 2){
                                $this->error('课程已完成，该请假已过期');
                            }
                            //相同则改为已请假
                            TeacherClassDtl::where(['id'=>$v['id']])->update(['status'=>3]);
                            AskForLeave::where(['id'=>$params['vacation_id']])->update(['bk_time'=>$v['time_start'],'bk_class_num'=>$v['class_num']]);
                        }
                    }
                }
                //给教师发已通过
                $send_add_notice = [];
                $send_add_notice['title'] = '请假审批结果通知';//标题
                $send_add_notice['content'] = "您的请假已通过";//内容
                $send_add_notice['need_confirm'] = 2;//是否需要确认 1是 2否
                $send_add_notice['jump'] = '课程详情';//跳转地址
                $send_add_notice['teacher_class_id'] = $find['teacher_class_id'];//课程id
                $send_add_notice['teacher_class_dtl_id'] = 0;//课程安排id
                $send_add_notice['send_user_id'] = $find['get_user_id'];//发送人id
                $send_add_notice['get_user_id'] = $find['send_user_id'];//接收人id
                $send_add_notice['identity_type'] = '教师';//接收人身份
                $send_add_notice['addtime'] = date('Y-m-d H:i:s');//添加时间
                $send_add_notice['is_read'] = 1;//是否已读 1未读 2已读
                Notice::insertGetId($send_add_notice);
                // 发送个推消息
                Notice::sendGetuiPush($send_add_notice);
                break;
            case 3:
                //驳回
                $msg = '已驳回';
                AskForLeave::where(['id'=>$params['vacation_id']])->update(['status'=>3]);
                //给教师发被驳回
                $send_add_notice = [];
                $send_add_notice['title'] = '请假审批结果通知';//标题
                $send_add_notice['content'] = "您的请假已驳回";//内容
                $send_add_notice['need_confirm'] = 2;//是否需要确认 1是 2否
                $send_add_notice['jump'] = '课程详情';//跳转地址
                $send_add_notice['teacher_class_id'] = $find['teacher_class_id'];//课程id
                $send_add_notice['teacher_class_dtl_id'] = 0;//课程安排id
                $send_add_notice['send_user_id'] = $find['get_user_id'];//发送人id
                $send_add_notice['get_user_id'] = $find['send_user_id'];//接收人id
                $send_add_notice['identity_type'] = '教师';//接收人身份
                $send_add_notice['addtime'] = date('Y-m-d H:i:s');//添加时间
                $send_add_notice['is_read'] = 1;//是否已读 1未读 2已读
                Notice::insertGetId($send_add_notice);
                // 发送个推消息
                Notice::sendGetuiPush($send_add_notice);
                break;
            default:
                $this->error('确认状态参数异常');
                break;
        }
        Notice::where(['id'=>$params['id']])->update(['confirm_status'=>$params['vacation_status']]);
        $this->success('审核请假',$msg);
    }

    //消息已读
    public function read_notice(){
        $params = $this->request->param();
        if(!isset($params['ids'])||!$params['ids']){
            $this->error('消息id必传');
        }
        $user = $this->auth->getUser();
        if(!isset($user['id'])||!$user['id']){
            $this->error('当前登录用户不存在');
        }

        if (is_array($params['ids'])) {
            $ids = $params['ids'];
        } else {
            $ids = explode(',', $params['ids']);
        }
        $ids = array_filter(array_map('intval', $ids));
        if (empty($ids)) {
            $this->error('消息id格式错误');
        }

        $affect = Notice::where('get_user_id', $user['id'])
            ->whereIn('id', $ids)
            ->update(['is_read' => 2]);

        if ($affect === false) {
            $this->error('标记已读失败');
        }
        $this->success('标记已读成功', ['updated' => $affect]);
    }

    //消息全部已读
    public function all_read_notice(){
        $user = $this->auth->getUser();
        if(!isset($user['id'])||!$user['id']){
            $this->error('当前登录用户不存在');
        }

        $affect = Notice::where(['get_user_id' => $user['id'], 'is_read' => 1])
            ->update(['is_read' => 2]);
        if ($affect === false) {
            $this->error('标记已读失败');
        }
        $this->success('全部标记已读成功', ['updated' => $affect]);
    }
}
